package com.woniuxy.service.impl;

import com.woniuxy.dao.ProductDao;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author: 邓宇昂
 * @Do: TODO
 * @DateTime: 22/05/30/0030 下午 12:06
 * @Component: 成都蜗牛学苑
 **/
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDao productDao;

    @Override
    public Product addOne(Product product) {
        return  productDao.save(product);
    }

    @Override
    public ResponseEntity findOneById(Integer pid) {
        Optional<Product> byId = productDao.findById(pid);
        //有数据才返回
        if (byId.isPresent()) {
            return new ResponseEntity(byId.get());
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @Override
    public ResponseEntity update(Product product) {
        //save方法是更新和新增合并的方法，有id就是更新，没有就是新增
        ResponseEntity response = new ResponseEntity(productDao.save(product));
        int i = 1/0;
        return response;
    }

    @Override
    public ResponseEntity findAllByNameOrPrice(String name, double minPrice, double maxPrice) {
        List<Product> products = productDao.findByNameOrPriceBetween(name,minPrice,maxPrice);
        return new ResponseEntity(products);
    }
}
